<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\web\JsExpression;

$from = Yii::$app->request->get('op');
/* @var $this yii\web\View */
/* @var $model common\models\business\BusinessBackpig */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="row">
    <div class="col-lg-12">
        <section class="panel">
        <div class="panel-body">
            <?php $form = ActiveForm::begin([
                'options' => ['class' => 'form-horizontal','enctype' => 'multipart/form-data'],
                'fieldConfig' => [
                    'template' => "<div class='col-xs-3 col-sm-2 text-right'>{label}</div><div class='col-xs-9 col-sm-7'>{input}</div><div class='col-xs-12 col-xs-offset-3 col-sm-3 col-sm-offset-0'>{error}</div>",
                ]
            ]); ?>

            <div class="adv-table editable-table ">
                <div class="business-backpig-form">
                   <?= $form->field($model, 'number')->textInput(['value'=>$model->isNewRecord?date('YmdHis').rand(100,999):$model->number,'readonly'=>'true']) ?>

                        <?= $form->field($model, 'pigid')->widget(\kartik\select2\Select2::className(), [
                            'options' => ['placeholder' => '请输入回购仔猪编号，农户或者业务员姓名...'],
                            'initValueText'=>!$model->pig?'':$model->pig->no,
                            'disabled' => $model->id&&$from?true:false,
                            'pluginOptions' => [
                                'allowClear' => true,
                                'minimumInputLength' => 1,//重要
                                'language' => [
                                    'errorLoading' => new JsExpression("function () { return 'Waiting...'; }"),
                                ],
                                'ajax' => [
                                    'url' => '/business/search/search-backpig',
                                    'dataType' => 'json',
                                    'data' => new JsExpression('function(params) { return {q:params.term}; }'),
                                    'cache' => true
                                ],
                                'width' => '400px',
                                'escapeMarkup' => new JsExpression('function (markup) { console.log(markup);return markup;}'),
                                'templateResult' => new JsExpression('function(res) { console.log(\'templateResult\');return res.text;}'),
                                'templateSelection' => new JsExpression('function (res) { 
                                    return res.text;
                                }'),
                            ],
                        ])->label('回购仔猪');
                        ?>

                        <?= $form->field($model, 'farmername')->textInput(['maxlength' => true,'readonly'=>true]) ?>

                        <?= $form->field($model, 'grossweight')->textInput(['maxlength' => true]) ?>

                        <?= $form->field($model, 'backprice')->textInput(['maxlength' => true]) ?>

                        <?= $form->field($model, 'backtotal')->textInput(['maxlength' => true,'readonly'=>true]) ?>



                        <input type="hidden" id="businessbackpig-farmerid" name="BusinessBackpig[farmerid]" value="<?= $model->farmerid ?>">
                </div>
            </div>
            <div class="row">
                <div class="col-md-10 col-md-offset-1">
                    <table class="table table-striped" id="cost">
                        <tr>
                            <th style="width: 10%">成本类型</th>
                            <th style="width: 10%">金额</th>
                            <th style="width: 10%">时间</th>
                            <th style="width: 10%">上报人员</th>
                            <th style="width: 10%">状态</th>
                            <th style="width: 20%">备注</th>
                            <th style="width: 10%">操作</th>
                        </tr>
                        <?php $total=0; ?>
                        <?php foreach ($cost as $key => $row) { ?>
                        <tr class="costdata">
                            <input type="hidden" name="costid[]" value="<?= $row['id'] ?>">
                            <td><input type="text" name="costtype[]" class="form-control" value="<?= $row['type'] ?>" disabled=true></td>
                            <td><input type="number" name="price[]"  class="form-control" value="<?= $row['price'] ?>" disabled=true></td>
                            <td><input type="text"  class="form-control" name="createtime[]" value="<?= $row['createtime']?>"  disabled=true></td>
                            <td><input type="text" name="person[]"  class="form-control" value="<?= $row['person'] ?>" disabled=true></td>
                            <td>
                                <select name="status[]" id="" class="form-control">
                                    <?php foreach (\common\models\business\BusinessCost::$statues as $item => $val) { ?>
                                        <option value="<?= $item ?>" <?php if($val==$row['status']) { ?> selected <?php } ?> ><?= $val ?></option>
                                    <?php } ?>
                                </select>
                            </td>
                            <td><input type="text" name="description[]"  class="form-control" value="<?= $row['description'] ?>" disabled=true></td>

                            <td>
                                <div style="float: left;width: 13%;margin-left: 1%">
                                    <?= Html::button('删除此项',['class'=>'btn btn-warning btn-sm del']) ?>
                                </div>
                            </td>
                        </tr>
                        <?php } ?>
                        <tr class="costdata" id="zongji">
                            <td><b>总计</b></td>
                            <td><b><?= $total ?></b></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                        </tr>
                    </table>

                    <div class="form-group">
                        <?= Html::button('添加一项明细',['id'=>'ccopy','class'=>'btn btn-info']) ?>
                        <?= Html::submitButton($model->isNewRecord ? '确认提交' : '确认更新', ['class' => $model->isNewRecord ? 'btn btn-success sub' : 'btn btn-primary sub']) ?>
                    </div>

                </div>
            </div>
            <?php ActiveForm::end(); ?>
        </div>
    </section>
</div>
</div>




<script>

    <?php $this->beginBlock('js_end') ?>

    $(document).ready(function() {

        $('input[name="pig[]"]').keyup(function () {
            shishiSearch(this)
        });

        var str = '<tr class="costdata"> <td><select name="costtype[]" class="form-control"> <?php foreach ($costtype as $item => $val) { ?> <option value="<?= $val['id'] ?>" ><?= $val['name'] ?></option> <?php } ?></select></td> <td><input type="text" name="price[]"  class="form-control" value=""></td> <td><input type="date"  class="form-control" name="createtime[]" value="<?= date('Y-m-d') ?>"></td> <td><input type="text" name="person[]" disabled=true value="<?= Yii::$app->user->identity->username ?>"  class="form-control" value=""></td> <td> <select name="status[]" id="" class="form-control"> <?php foreach (\common\models\business\BusinessCost::$statues as $item => $val) { ?> <option value="<?= $item ?>"><?= $val ?></option> <?php } ?> </select> </td><td><input type="text" name="description[]"  class="form-control" value=""></td> <td> <div style="float: left;width: 13%;margin-left: 1%"> <?= Html::button('删除此项',['class'=>'btn btn-warning btn-sm del']) ?> </div> </td> </tr>';
        //添加一项
        $('#ccopy').click(function () {
            $('#zongji').before(str);
            $('.del').unbind('click').click(function () {
                if (confirm('确定要删除吗？')) {
                    $(this).parent().parent().parent('.costdata').remove();
                    return;
                }
            });
        });

        //删除一项
        $('.del').unbind('click').click(function () {
            if (confirm('确定要删除吗？')) {
                $(this).parent().parent().parent('.costdata').remove();
            }
        });

    });

    <?php $this->endBlock(); ?>
    <?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>

</script>




<script>
    <?php $this->beginBlock('js_end') ?>

        $('#businessbackpig-pigid').change(function(){
//            console.log($(this).val());
            $.post('/business/backpig/searchcost',{pigid:$(this).val()},function(res){
                var str = '';
//                $(".costdata").remove();
                $(res.data).each(function(index,v){

//                    str += '<tr class="costdata"> <input type="hidden" name="costid[]" value="'+v.type+'"> <td><input type="text" name="costtype[]" class="form-control" value="'+v.type+'" disabled=true></td> <td><input type="number" name="price[]"  class="form-control" value="'+v.price+'" disabled=true></td> <td><input type="text"  class="form-control" name="createtime[]" value="'+v.createtime+'"  disabled=true></td> <td><input type="text" name="person[]"  class="form-control" value="'+v.person+'" disabled=true></td> <td><input type="text" name="status[]"  class="form-control" value="'+v.status+'" ></td> <td><input type="text" name="description[]"  class="form-control" value="'+v.description+'" disabled=true></td> <td> <div style="float: left;width: 13%;margin-left: 1%"> <?//= Html::button('删除此项',['class'=>'btn btn-warning btn-sm del']) ?>// </div> </td> </tr>';
                    str += '<tr class="costdata"> <input type="hidden" name="costid[]" value="'+v.id+'"> <td><input type="text" name="costtype[]" class="form-control" value="'+v.type+'" disabled=true></td> <td><input type="number" name="price[]"  class="form-control" value="'+v.price+'" disabled=true></td> <td><input type="text"  class="form-control" name="createtime[]" value="'+v.createtime+'"  disabled=true></td> <td><input type="text" name="person[]"  class="form-control" value="'+v.person+'" disabled=true></td> <td> <select name="status[]"  class="form-control"> <option value=1>确认支出</option> <option value=0>不确认支出</option> </select> <td><input type="text" name="description[]"  class="form-control" value="'+v.description+'" disabled=true></td> <td> <div style="float: left;width: 13%;margin-left: 1%"> <?= Html::button('删除此项',['class'=>'btn btn-warning btn-sm del']) ?> </div> </td> </tr>';
                });
                console.log(str);
                $('#zongji').before(str);
                $('#businessbackpig-farmername').val(res.farmername);
                $('#businessbackpig-farmerid').val(res.farmerid);

                $('.del').unbind('click').click(function () {
                    if (confirm('确定要删除吗？')) {
                        $(this).parent().parent().parent('.costdata').remove();
                        return;
                    }
                });


            },'json')
        })
    <?php $this->endBlock(); ?>
    <?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>
</script>